/*9oNSQ86zYBKgm3JBJdBE6B9mSqORySurWsPxYgnmaj9ogKuzVLrRF3U92kD94pqx*/
/*
 * @Author: xiahui 
 * @PageInfo: 表头编辑后事件 
 * @Date: 2019-01-14 16:49:19 
 * @Last Modified by: zhr
 * @Last Modified time: 2021-10-14 10:42:04
 */
import { ajax } from 'nc-lightapp-front';
import { PAGECODE, AREA, URL, FIELDS, CARDTABLEAREAIDS, BUTTONID, DEFCACHEKEY } from '../../constance';
import { showWarningDialog } from '../../../../../scmpub/scmpub/pub/tool/messageUtil';
import { clearCardData, setCardPageForEmptyPk_org_v } from '../../utils/cardPageUtil';
import {
	createExtBillHeadAfterEventData,
	processExtBillCardHeadEditResult
} from '../../../../../scmpub/scmpub/pub/tool/afterEditUtil';
import transtypeUtils from '../../../../../scmpub/scmpub/pub/tool/transtypeUtils';
import { showWarningInfo } from '../../../../../scmpub/scmpub/pub/tool/messageUtil';
import { getLangByResId } from '../../../../../scmpub/scmpub/pub/tool/multiLangUtil';
import { getDefData } from '../../../../../scmpub/scmpub/pub/cache/cacheDataManager';
import { setBusinessEmail } from '../../../../../uapbd/supplier/supplier_base/utils/SupplierCommonUtils'

const { setValue } = transtypeUtils;

export default function(props, moduleId, key, value, oldVal, refInfo) {
	if (key == FIELDS.pk_org_v) {
		// 主组织切换事件
		if (!value.value) {
			// 主组织为空
			showWarningDialog(
				getLangByResId(this, '4004PURDAILY-000000'),
				getLangByResId(this, '4004PURDAILY-000001'),
				{
					/* 国际化处理： 确认修改,是否修改组织，这样会清空您录入的信息？*/
					beSureBtnClick: () => {
						clearCardData(props); // 清空卡片数据
						setCardPageForEmptyPk_org_v(props);
						supplyTransType.call(this, props, key);
					},
					cancelBtnClick: () => {
						props.form.setFormItemsValue(moduleId, {
							pk_org_v: { value: oldVal.value, display: oldVal.display }
						});
					}
				}
			);
		} else if (value.value) {
			if (oldVal && oldVal.value) {
				// 主组织切换前，有数据
				showWarningDialog(
					getLangByResId(this, '4004PURDAILY-000000'),
					getLangByResId(this, '4004PURDAILY-000001'),
					{
						/* 国际化处理： 确认修改,是否修改组织，这样会清空您录入的信息？*/
						beSureBtnClick: () => {
							props.form.EmptyAllFormValue(AREA.cardFormId); //订单
							props.form.setFormItemsValue(AREA.cardFormId, {
								[FIELDS.pk_org_v]: { value: value.value, display: value.display }
							});
							CARDTABLEAREAIDS.forEach((areaId) => {
								props.cardTable.setTableData(areaId, { rows: [] });
							});
							props.cardTable.addRow(
								AREA.cardTableId,
								0,
								{ crowno: { display: '10', value: '10' } },
								false
							);
							let data = createExtBillHeadAfterEventData(
								props,
								PAGECODE.cardPagecode,
								AREA.cardFormId,
								CARDTABLEAREAIDS,
								moduleId,
								key,
								value
							);
							doAction.call(this, props, data, key);
						},
						cancelBtnClick: () => {
							props.form.setFormItemsValue(moduleId, {
								pk_org_v: { value: oldVal.value, display: oldVal.display }
							});
						}
					}
				);
			} else {
				props.resMetaAfterPkorgEdit();
				let data = createExtBillHeadAfterEventData(
					props,
					PAGECODE.cardPagecode,
					AREA.cardFormId,
					CARDTABLEAREAIDS,
					moduleId,
					key,
					value
				);
				doAction.call(this, props, data, key);
			}
		}
	} else if (
		key == FIELDS.cvendorvid ||
		key == FIELDS.subscribedate ||
		key == FIELDS.valdate ||
		key == FIELDS.invallidate ||
		key == FIELDS.ctrantypeid ||
		key == FIELDS.nexchangerate ||
		key == FIELDS.nglobalexchgrate ||
		key == FIELDS.ngroupexchgrate ||
		key == FIELDS.corigcurrencyid ||
		key == FIELDS.pk_payterm ||
		key == FIELDS.personnelid ||
		key == FIELDS.cratetype
	) {
		let reqData = createExtBillHeadAfterEventData(
			props,
			PAGECODE.cardPagecode,
			AREA.cardFormId,
			CARDTABLEAREAIDS,
			moduleId,
			key,
			value,
			{}
		);
		doAction.call(this, props, reqData, key);
		//供应商修改后设置业务邮箱		
		if(key == FIELDS.cvendorvid){
			setBusinessEmail(props, value,AREA.cardFormId,FIELDS.businessemail);
		}
	}

	if(key == 'vdef4'){
		console.log("预提频率控制次数")
		if(value.refcode == "1002"){
			props.form.setFormItemsDisabled(moduleId,{'vdef5':false});
			props.form.setFormItemsRequired(moduleId,{'vdef5':true});
		}
		if(value.refcode == "1001"){
			props.form.setFormItemsRequired(moduleId,{'vdef5':false});
			props.form.setFormItemsValue(moduleId,{'vdef5':{value:null}});
			props.form.setFormItemsDisabled(moduleId,{'vdef5':true});
		}
	}
}

function doAction(props, data, key) {
	ajax({
		url: URL.headAfterEdit,
		data: data,
		async: false,
		success: (res) => {
			if (res.success && res.data) {				
				processExtBillCardHeadEditResult(props, AREA.cardFormId, CARDTABLEAREAIDS, res.data);
				supplyTransType.call(this, props, key);
				if (res.data.userObject.error && key != FIELDS.ctrantypeid) {
					//修改合同生效日期编辑后事件
					this.props.form.setFormItemsValue(AREA.cardFormId, { [key]: data.oldvalue });
					showWarningInfo(null, res.data.userObject.error);
				}
			}
		}
	});
}

/**
 * 设置业务邮箱
 * @param {*} props 
 * @param {供应商pk} data 
 */
// function setBusinessEmail(props, data) {
// 	ajax({
// 		url: '/nccloud/ct/purdaily/setBusinessEmail.do',
// 		data: {userJson:data},
// 		async: false,
// 		success: (res) => {
// 			if (res.success && res.data) {
// 				this.props.form.setFormItemsValue(AREA.cardFormId, {
// 					[FIELDS.businessemail]: { value: res.data.businessEmail, display: null }
// 				});
// 			}
// 		},		
// 	});
// }

/**
 * 设置交易类型
 * @param {*} props 
 */
function supplyTransType(props, key) {
	// 设置默认交易类型(发布交易类型后, 报账平台)
	if (FIELDS.pk_org_v == key) {
		setValue.call(this, AREA.cardFormId, FIELDS.ctrantypeid, FIELDS.vtrantypecode);
		props.button.setDisabled({
			[BUTTONID.AddLine]: false,
			[BUTTONID.ReRangeRowNo]: false
		});
		// 报账平台，默认交易类型值
		let transtypecode = getDefData(DEFCACHEKEY.transTypeCacheKey, 'transtypecode');
		if (transtypecode) {
			let billtypeid = getDefData(DEFCACHEKEY.transTypeCacheKey, 'billtypeid');
			let billtypename = getDefData(DEFCACHEKEY.transTypeCacheKey, 'billtypename');
			props.form.setFormItemsValue(AREA.cardFormId, {
				[FIELDS.ctrantypeid]: { value: billtypeid, display: billtypename },
				[FIELDS.vtrantypecode]: { value: transtypecode, display: transtypecode }
			});
		}
	}
}

/*9oNSQ86zYBKgm3JBJdBE6B9mSqORySurWsPxYgnmaj9ogKuzVLrRF3U92kD94pqx*/